package com.wisemoresystem.seal.action.frame;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_role;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MenuService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.SessionVal;
import com.wisemoresystem.seal.web.WebTools;
/**
 * 主页面
 * @author Administrator
 *
 */
@Controller("ixFrame")
@Scope("prototype")
@RequestMapping("/ix")
public class FrameAction extends ProjBaseAction {

	@Autowired
	private MenuService menuService;

	@RequestMapping("/frame_before")
	public ModelAndView frame_before() {

		ModelAndView mv = new ModelAndView("/jsp/frame/frame_before");

		return mv;
	}

	@RequestMapping("/frame")
	public ModelAndView frame() {

		ModelAndView mv = new ModelAndView("/jsp/frame/frame");

		return mv;
	}
	/**
	 * 头部机构
	 * @return
	 */
	@RequestMapping("/header")
	public ModelAndView header() {

		t_bank bank = (t_bank) oc.findObjById(WebTools.getCurrentBankId(),
				t_bank.class);
		String bankName = bank.getName();

		ModelAndView mv = new ModelAndView("/jsp/frame/header");
		mv.addObject("bankName", bankName);
		return mv;
	}
	/**
	 * 页面左侧树列表菜单
	 * @return
	 */
	@RequestMapping("/left")
	public ModelAndView left() {
		t_user user = WebTools.getCurrentUser();

		List<Map<String, Object>> menuList;

		Set<String> allcodes = new HashSet<String>();

		if (ChkTools.isNotNull(user.getRole_id())) {
			t_role role = menuService.findObjById(user.getRole_id(),
					t_role.class);
			menuList = menuService.selectMenuList(role.getAuth_codes());

			String codes = role.getAuth_codes();
			String[] alcod = codes.split(",");
			for (String string : alcod) {
				allcodes.add(string);
			}

		} else {
			menuList = menuService.selectMenuList();
			for (Map<String, Object> map : menuList) {
				allcodes.add(map.get("code") + "");
			}
		}
		req.getSession().setAttribute(SessionVal.role_codes, allcodes);
		String menuJson = JsonTools.toJson(menuList);

		System.err.println(WebTools.getCurrentRoleCodes());
		ModelAndView mv = new ModelAndView("/jsp/frame/left");
		mv.addObject("menuJson", menuJson);
		return mv;
	}
	
	@RequestMapping("/main")
	public ModelAndView main() {
		page.setPageSize(5);

		ModelAndView mv = new ModelAndView("/jsp/frame/main");
		return mv;
	}

	@RequestMapping("/get_user")
	public void get_user() throws IOException {
		t_user user = WebTools.getCurrentUser();
		String islive = "none";
		if (ChkTools.isNotNull(user))
			islive = "live";
		resp.getWriter().print(islive);
		resp.getWriter().flush();
	}

}
